﻿#region License

// Another free, open solution by folks who get it.
// Copyright © 2011 Business in Unison, Inc.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;

namespace Patterns.Cache
{
	/// <summary>
	/// 	Defines an interface for objects that provide a cache implementation.
	/// </summary>
	public interface ICache
	{
		/// <summary>
		/// 	Gets the <see cref = "System.Object" /> with the specified key.
		/// </summary>
		object this[string key] { get; set; }

		/// <summary>
		/// Gets the item with the specified key.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="key">The key.</param>
		/// <param name="factory">The factory to run in case a replacement item is
		/// needed.</param>
		/// <param name="absoluteExpiration">The absolute expiration.</param>
		/// <param name="slidingExpiration">The sliding expiration.</param>
		/// <returns></returns>
		/// <remarks>Supplying a replacement factory causes the factory's result to be
		/// cached before being returned.</remarks>
		T Get<T>(string key, Func<T> factory = null, DateTime? absoluteExpiration = null, TimeSpan? slidingExpiration = null) where T : class;

		/// <summary>
		/// Adds the instance to the cache with the specified key.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="key">The key.</param>
		/// <param name="instance">The instance.</param>
		/// <param name="absoluteExpiration">The absolute expiration.</param>
		/// <param name="slidingExpiration">The sliding expiration.</param>
		/// <returns></returns>
		ICache Set<T>(string key, Func<T> instance, DateTime? absoluteExpiration = null, TimeSpan? slidingExpiration = null) where T : class;

		/// <summary>
		/// 	Removes the item with the specified key.
		/// </summary>
		/// <param name = "key">The key.</param>
		ICache Remove(string key);

		/// <summary>
		/// Called when an item is added or removed from the cache.
		/// </summary>
		/// <param name="key">The key.</param>
		/// <param name="action">The action.</param>
		/// <returns></returns>
		ICache OnUpdate(string key, Action<CacheEvent> action);
	}
}